_$.Form=function(target){
    target = _$.getTarget(target);
    var _this = this;
    _this.el = target;

    var opts = {
    };
    _this.options = $.extend(true,opts);
};

_$.Form.prototype = {
    validState:true,
    clear:function(){
        var _this = this;
        var formItems =  _this.getFields(_this.el);
        if(formItems !== null  && formItems.length>0){
            for(var i=0;i<formItems.length;i++){
                var key = formItems[i];
                var obj = _$.getById(key);
                if(obj){
                    obj.setValue(null);
                }
            }
        }
    },
    reset:function(){
        var _this = this;
        _this.clear();
        var formItems =  _this.getFields(_this.el);
        if(formItems !== null  && formItems.length>0){
            for(var i=0;i<formItems.length;i++){
                var key = formItems[i];
                var obj = _$.getById(key);
                if(obj){
                    obj.reset();
                }
            }
        }
    },
    setValue:function(values,func){
        var _this = this;
        _this.clear();
        var formItems =  _this.getFields(_this.el);
        for(var i=0;i<formItems.length;i++){
            var obj = formItems[i];
            if(obj != null){

                var key = obj.getOption("name") || obj.id;
                var value = values[key];
                obj.setValue(value);
            }
        }
    },
    getValue:function(){
        var _this = this;
        var formItems =  _this.getFields(_this.el);
        var values = {};
        for(var i=0;i<formItems.length;i++){
            var obj = formItems[i];
            if(obj !== null){
                var value = obj.getValue();
                if(isNotEmpty(value)){
                    var name = obj.getOption("name") || obj.id;
                    values[name] = value;
                }
            }
        }
        return values;
    },
    getEditData:function(){
        var _this = this;
        var formItems =  _this.getFields(_this.el);
        var values = {};
        for(var i=0;i<formItems.length;i++){
            var key = formItems[i];
            var obj = _$.getById(key);
            if(obj !== null){
                obj.getEditValue(values);
            }
        }
        return values;
    },
    getAddData:function(){
        var _this = this;
        var values = {};
        var current = _this.getValue();
        for(var key in current){
            var c_val = current[key];
            if(isNotEmpty(c_val)){
                values[key] = c_val;
            }
        }
        return values;
    },
    getQueryData:function(){
        var _this = this;
        var formItems =  _this.getFields(_this.el);
        var values = {};
        for(var i=0;i<formItems.length;i++){
            var _id = formItems[i];

            if((_id+"").indexOf("_type") != -1){
                continue;
            }
            var obj = _$.getById(_id);
            if(obj != null){
                var value = obj.getValue();
                if(isNotEmpty(value)){
                    var name = obj.getOption("name") || _id;
                    values[name] = value;

                    var typeId = _id + "_type";
                    var typeObj = _$.getById(typeId);

                    if(typeObj!= null){
                        var typeValue = typeObj.getValue();
                        if(isNotEmpty(typeValue)){
                            var typeName = typeObj.getOption("name");
                            values[typeName] = typeValue;
                        }
                    }
                }
            }
        }
        return values;
    },
    getFields:function(){
        var _this = this;
        var items = _$.findControls(_this.el);

        var list = [];
        for(var i=0;i<items.length;i++){
            var item = items[i];
            if(item.isFormItem){
                list.add(item);
            }
        }
        return list;
    },
    setValid:function(flag){
        var _this = this;
        _this.validState = flag;
        var formItems = _this.getFields();
        for(var i=0;i<formItems.length;i++){
            var obj = formItems[i];
            obj.setValid(flag);
        }
    },
    isValid:function(){
        var _this = this;
        _this.validState = true;
        var formItems = _this.getFields();
        for(var i=0;i<formItems.length;i++){
            var obj = formItems[i];
            var flag = obj.isValid();
            if(_this.validState && !flag){
                _this.validState = flag;
            }
        }
        return _this.validState;
    }
};